﻿using AIO.Entity.Base;
using AIO.IService.Base;
using AIO.Service.Base;
using Microsoft.EntityFrameworkCore;

namespace AIO.Api.Tools.Extend
{
    /// <summary>
    /// 服务扩展
    /// </summary>
    public static class ServiceExtensions
    {

        /// <summary>
        /// 核心策略配置
        /// </summary>
        /// <param name="services"></param>
        public static void ConfigureCors(this IServiceCollection services)
        {
            // 添加跨域服务
            services.AddCors(options =>
            {
                //添加跨域策略
                options.AddPolicy("AnyPolicy",
                    builder => builder
                    //.WithOrigins("http://localhost:5173/")
                    .AllowAnyOrigin()// 任何源
                    .AllowAnyMethod()// 任何方法
                    .AllowAnyHeader()// 任何请求头
                    );
            });
        }
        /// <summary>
        /// MySql数据库配置
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        public static void ConfigureMySqlContext(this IServiceCollection services, IConfiguration config)
        {
            var connectionString = config.GetConnectionString("ApiDB");
            services.AddDbContext<ApiDBContext>(
                builder => builder.UseMySql(connectionString, MySqlServerVersion.LatestSupportedServerVersion)
                );
        }
        /// <summary>
        /// 仓库的包装器配置
        /// </summary>
        /// <param name="services"></param>
        public static void ConfigureRepositoryWrapper(this IServiceCollection services)
        {
            services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();

        }
    }
}
